import {getLocalStorage, setLocalStorage} from "@/utils/storageUtil.ts";
import {formatRegions} from "@/utils/format.ts";

/**
 * 用户登录存储相关信息
 *
 * @author Finn Yu
 * @since 2023/7/5 13:42
 */

const AUTH_EXPIRES = 24 * 60 * 60 * 1000;

export function setToken(token: string) {
    setLocalStorage('token', token, AUTH_EXPIRES)
}

export function getToken() {
    return getLocalStorage('token')
}

export function setCurrentUser(user: object) {
    setLocalStorage('currentUser', user, AUTH_EXPIRES)
}

export function getCurrentUser() {
    return getLocalStorage('currentUser')
}

export function setRegions(regions: any) {
    setLocalStorage("regions", formatRegions(regions));
}

export function getRegions() {
    return getLocalStorage("regions")
}

export function setProjectConfig(config: any) {
    setLocalStorage("projectConfig", config, AUTH_EXPIRES);
}

export function getProjectConfig() {
    return getLocalStorage("projectConfig");
}


export function clearAuthInfo() {
    localStorage.removeItem('token');
    localStorage.removeItem('currentUser');
    localStorage.removeItem('regions');
}

export function isLogin() {
    return getToken() && getCurrentUser();
}

